Summary of php's method of judging and's calculation of leap year [three methods]

  • 2021-12-12 08:16:58
  • OfStack

In this paper, the method of judging/calculating leap year by php is described with examples. Share it for your reference, as follows:

1. php leap year calculation method 1:

(1) The ordinary year can be divisible by 4 but not by 100 is a leap year. (For example, 2004 is a leap year, and 1900 is not a leap year)

② The century year can be divisible by 400 but not by 3200 is a leap year. (For example, 2000 is a leap year and 3200 is not a leap year)


$year=mt_rand(1900,2200);// From 1900 Year to 2200 , you can change it yourself, or you can give it to 1 A fixed value. 
if($year%100==0){// Judge the year of the century 
if ($year%400==0&&$year%3200!=0){
  echo " Century year ".$year." It's a leap year !";// Leap year in the century year 
}
else{echo " Century year ".$year." Not a leap year !";}
}
else{// The rest is the ordinary year 
  if($year%4==0&&$year%100!=0){
  echo " Ordinary year ".$year." It's a leap year !";// Leap year in ordinary year 
}
else {echo " Ordinary year ".$year." Not a leap year !";}
}

2. php method for judging leap year 2:


$year = 2008;// It can be like the above example 1 Sample use mt_rand Random take 1 Years, you can also assign values at will. 
$time = mktime(20,20,20,4,20,$year);// Acquire 1 Of a date  Unix  Time stamp ;
if (date("L",$time)==1){ // Format the time and determine whether it is a leap year, and the following is equal to 1 It can also be omitted; 
echo $year." It's a leap year ";
}else{
echo $year." Not a leap year ";
}

3. php Method for Judging Leap Year and Calculating Leap Year 3:


$year = 2000;
$time = mktime(20,20,20,2,1,$year);// Acquire 1 Of a date  Unix  Time stamp ;
if (date("t",$time)==29){ // Format the time and determine the 2 Is the month 29 Days; 
echo $year." It's a leap year ";// Yes 29 Leap year when the day is exported; 
}else{
echo $year." Not a leap year ";
}

PS: Here are some time and date related tools for your reference:

Online date/day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online date calculator/difference days calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online Date-Day Difference Calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Date and Time Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Introduction to php Object-Oriented Programming", "Introduction to php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: